package com.itheima.tliaswebmanagement.controller;

import com.itheima.tliaswebmanagement.pojo.Emp;
import com.itheima.tliaswebmanagement.pojo.EmpQueryParam;
import com.itheima.tliaswebmanagement.pojo.PageResult;
import com.itheima.tliaswebmanagement.pojo.Result;
import com.itheima.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询,{},{},{},{},{},{}", empQueryParam);
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Emp> empList= empService.findAll();
        return Result.success(empList);
    }


    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("保存员工,{}", emp);
        empService.save(emp);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids) {
        //@RequestParam的几个用法（作用）
        /**
         * 1.前端 参数名称 和 形参名称 不一致
         * 2.设置默认值（分页查询里）
         * 3.用集合接收数组参数
         */
        log.info("批量删除员工,{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }


    //查询回显
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("查询员工信息,{}", id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息,{}", emp);
        empService.update(emp);
        return Result.success();
    }
}
